package com.oa.user.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.oa.oaspccommon.po.User;

import com.oa.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zd
 * @date 2021年04月16日 10:40
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 查询所有user
     * @return
     */
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public List<User> findAll(){
        return userService.findAll();
    }


    /**
     * 根据id查询
     * @param uid
     * @return
     */
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public User finfOne(@RequestParam("uid") Integer uid){
        return userService.findOne(uid);
    }

    /**
     * 新增
     * @param user
     * @return
     */
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public Boolean addUser(@RequestBody User user){
        int i = userService.addUser(user);
        if(i>0){
            return true;
        }
        return false;
    }

    /**
     * 删除
     * @param uid
     * @return
     */
    @RequestMapping(value = "/user",method = RequestMethod.DELETE)
    public Boolean delUSer(@RequestParam("uid") Integer uid){
        int i = userService.delUser(uid);
        if(i>0){
            return true;
        }
        return false;
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public Boolean update(@RequestBody User user){
        int i = userService.updateUser(user);
        if(i>0){
            return true;
        }
        return false;
    }


    /**
     * 根据username做模糊查询
     * @param username
     * @return
     */
    @RequestMapping(value = "/user2",method = RequestMethod.GET)
    public List<User> findByLike(@RequestParam("username") String username){
        return userService.findByLike(username);
    }


    /*  /**
     * 分页查询
     * @param user
     * @param pageCount
     * @param pageSize
     * @return
     */
    /*@RequestMapping(value = "users2",method = RequestMethod.GET)
       public PageInfo<User> findByPage(User user, @RequestParam(value = "pageCount") Integer pageCount, @RequestParam(value = "pageSize",defaultValue = "12")Integer pageSize){
        return userService.findByPage(user,pageCount,pageSize);
    }*/

    @RequestMapping(value = "/users2",method = RequestMethod.GET)
    //默认访问第一页
    public PageInfo<User> findByPage2(@RequestParam(defaultValue = "1", required = true, value = "pageNum") Integer pageNum){
        //每页12条数据
        Integer pageSize = 12;
        //分页查询,startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        PageHelper.startPage(pageNum,pageSize);
        List<User> all = userService.findAll();

        return new PageInfo<User>(all);
    }


}
